import { UnwrapRef } from "vue";

function defaultClone<T = any>(value: T): T {
  if (typeof value !== "object" || value === null) return value;
  return JSON.parse(JSON.stringify(value));
}

export default function useResettableRef<T>(value: T): [Ref<UnwrapRef<T>>, () => void] {
  const initialValue = defaultClone(value);
  const valueRef = ref<T>(value);
  function reset() {
    valueRef.value = defaultClone(initialValue) as UnwrapRef<T>;
  }
  return [valueRef, reset];
}
